package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AdminPreidCourse;
import com.ruoyi.system.domain.AdminSong;
import com.ruoyi.system.service.IAdminPreidCourseService;
import com.ruoyi.system.service.IAdminSongService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 曲目Controller
 * 
 * @author ruoyi
 * @date 2020-06-16
 */
@Controller
@RequestMapping("/system/song")
public class AdminSongController extends BaseController
{
    private String prefix = "system/song";

    @Autowired
    private IAdminSongService adminSongService;
    @Autowired
    private IAdminPreidCourseService adminPreidCourseService;

    @RequiresPermissions("system:song:view")
    @GetMapping()
    public String song()
    {
        return prefix + "/song";
    }

    /**
     * 查询曲目列表
     */
    @RequiresPermissions("system:song:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminSong adminSong)
    {
        startPage();
        List<AdminSong> list = adminSongService.selectAdminSongList(adminSong);
        return getDataTable(list);
    }

    /**
     * 导出曲目列表
     */
    @RequiresPermissions("system:song:export")
    @Log(title = "曲目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminSong adminSong)
    {
        List<AdminSong> list = adminSongService.selectAdminSongList(adminSong);
        ExcelUtil<AdminSong> util = new ExcelUtil<AdminSong>(AdminSong.class);
        return util.exportExcel(list, "song");
    }

    /**
     * 新增曲目
     */
    @GetMapping("/add/{bookId}")
    public String add(@PathVariable("bookId") String bookId,ModelMap mmap)
    {
    	mmap.put("bookId", bookId);
        return prefix + "/add";
    }

    /**
     * 新增保存曲目
     */
    @RequiresPermissions("system:song:add")
    @Log(title = "曲目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminSong adminSong)
    {
    	adminSong.setCompanyId(ShiroUtils.getCompanyId());
    	adminSong.setCompanyName(ShiroUtils.getCompanyName());
        adminSong.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(adminSongService.insertAdminSong(adminSong));
    }

    /**
     * 修改曲目
     */
    @GetMapping("/edit/{songId}")
    public String edit(@PathVariable("songId") Long songId, ModelMap mmap)
    {
        AdminSong adminSong = adminSongService.selectAdminSongById(songId);
        mmap.put("adminSong", adminSong);
        return prefix + "/edit";
    }

    /**
     * 修改保存曲目
     */
    @RequiresPermissions("system:song:edit")
    @Log(title = "曲目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminSong adminSong)
    {
        return toAjax(adminSongService.updateAdminSong(adminSong));
    }

    /**
     * 删除曲目
     */
    @RequiresPermissions("system:song:remove")
    @Log(title = "曲目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminSongService.deleteAdminSongByIds(ids));
    }
    
    /**
     * 曲目状态修改
     */
    @Log(title = "曲目管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:song:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(AdminSong adminSong)
    {
        return toAjax(adminSongService.changeStatus(adminSong));
    }

    /**
     * 新增保存曲目
     */
    @RequiresPermissions("system:song:add")
    @Log(title = "曲目", businessType = BusinessType.INSERT)
    @PostMapping("/add2")
    @ResponseBody
    public AjaxResult addSave2(AdminSong adminSong)
    {

      AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
      adminPreidCourse.setCourseId(adminSong.getSongId());
      adminPreidCourse.setCourseName(adminSong.getSongName());
      adminPreidCourse.setBookId(adminSong.getBookId());
      adminPreidCourse.setCourseType("1");
      adminPreidCourse.setPreidId(adminSong.getPreidId());
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());


      return toAjax(adminPreidCourseService.insertAdminPreidCourse(adminPreidCourse));
    }



  /**
   * 查询曲目列表
   */
  @RequiresPermissions("system:song:list")
  @PostMapping("/list2")
  @ResponseBody
  public TableDataInfo list2(AdminSong adminSong)
  {
    startPage();
    List<AdminSong> list = adminSongService.selectAdminSongList(adminSong);
    AdminPreidCourse adminPreidCourse = new AdminPreidCourse();
    for (AdminSong song : list) {
      adminPreidCourse.setCourseId(song.getSongId());
      adminPreidCourse.setPreidId(adminSong.getPreidId());
      adminPreidCourse.setCourseType("1");
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());
      List<AdminPreidCourse> adminPreidCourses = adminPreidCourseService.selectAdminPreidCourseList(adminPreidCourse);
      if(null!=adminPreidCourse && adminPreidCourses.size()>0){
        song.setAddPeriedFlag("1");
      }
      
    }
    return getDataTable(list);
  }

  /**
   * 删除课时曲目
   */
  @RequiresPermissions("system:song:remove")
  @Log(title = "曲目", businessType = BusinessType.DELETE)
  @PostMapping( "/remove2")
  @ResponseBody
  public AjaxResult remove2(AdminSong adminSong)
  {
    AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
    adminPreidCourse.setCourseId(adminSong.getSongId());
    adminPreidCourse.setPreidId(adminSong.getPreidId());
    adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());


    return toAjax(adminPreidCourseService.deleteAdminPreidCourse(adminPreidCourse));
  }

}
